package com.broad.commerce.code.modules;

import com.broad.commerce.code.modules.business.ControllerModule;
import com.broad.commerce.code.modules.business.InterfaceModule;
import com.broad.commerce.code.modules.business.ServiceModule;
import com.broad.commerce.code.modules.jdbc.EntityModule;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-02-08 23:43
 * @description: BusinessModule
 **/
@SuppressWarnings("DuplicatedCode")
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
@Getter
@Accessors(chain = true)
public class BusinessModule {
	@Setter
	private String modelPackage;
	@Setter
	private String jpaPackage;
	@Setter
	private String mybatisPackage;
	@Setter
	private String interfacePackage;
	@Setter
	private String servicePackage;
	@Setter
	private String controllerPackage;
	@Setter
	private EntityModule entityModule;
	private InterfaceModule interfaceModule;
	private ServiceModule serviceModule;
	private ControllerModule controllerModule;

	public void initBusinessModule(String mappingRootPath) {
		String modelClassName = this.entityModule.getClassName() + "DTO";
		this.setInterfaceModule(entityModule.getUpdate(), modelClassName);
		this.setServiceModule(entityModule.getUpdate(), modelClassName);
		this.setControllerModule(entityModule.getUpdate(), mappingRootPath, modelClassName);
	}

	private void setInterfaceModule(boolean update, String modelClassName) {
		InterfaceModule interfaceModule = new InterfaceModule();
		interfaceModule.setUpdate(update);
		interfaceModule.setPackageName(this.interfacePackage);
		interfaceModule.setClassName("I" + this.entityModule.getClassName() + "Service");
		interfaceModule.setEntityClassName(this.entityModule.getClassName());
		interfaceModule.setEntityFullClassName(this.entityModule.getFullClassName());
		interfaceModule.setModelClassName(modelClassName);
		interfaceModule.setModelFullClassName(this.getFullClassName(modelPackage, modelClassName));
		this.interfaceModule = interfaceModule;
	}

	private void setServiceModule(boolean update, String modelClassName) {
		ServiceModule serviceModule = new ServiceModule();
		String jpaClassName = this.entityModule.getClassName() + "Dao";
		String mybatisClassName = this.entityModule.getClassName() + "Mapper";
		serviceModule.setUpdate(update);
		serviceModule.setPackageName(this.servicePackage);
		serviceModule.setClassName(this.entityModule.getClassName() + "Service");
		serviceModule.setEntityClassName(this.entityModule.getClassName());
		serviceModule.setEntityFullClassName(this.entityModule.getFullClassName());
		serviceModule.setModelClassName(modelClassName);
		serviceModule.setModelFullClassName(this.getFullClassName(modelPackage, modelClassName));
		serviceModule.setInterfaceClassName(this.interfaceModule.getClassName());
		serviceModule.setInterfaceFullClassName(this.interfaceModule.getFullClassName());
		serviceModule.setJpaClassName(jpaClassName);
		serviceModule.setJpaFullClassName(this.getFullClassName(this.jpaPackage, jpaClassName));
		serviceModule.setMapperClassName(mybatisClassName);
		serviceModule.setMapperFullClassName(this.getFullClassName(this.mybatisPackage, mybatisClassName));
		this.serviceModule = serviceModule;
	}

	private void setControllerModule(boolean update, String mappingRootPath, String modelClassName) {
		ControllerModule controllerModule = new ControllerModule();
		controllerModule.setUpdate(update);
		controllerModule.setMappingRootPath(mappingRootPath);
		controllerModule.setPackageName(this.controllerPackage);
		controllerModule.setClassName(this.entityModule.getClassName() + "Controller");
		controllerModule.setEntityClassName(this.entityModule.getClassName());
		controllerModule.setEntityFullClassName(this.entityModule.getFullClassName());
		controllerModule.setModelClassName(modelClassName);
		controllerModule.setModelFullClassName(this.getFullClassName(modelPackage, modelClassName));
		controllerModule.setInterfaceClassName(this.interfaceModule.getClassName());
		controllerModule.setInterfaceFullClassName(this.interfaceModule.getFullClassName());
		controllerModule.setServiceClassName(this.serviceModule.getClassName());
		controllerModule.setServiceFullClassName(this.serviceModule.getFullClassName());
		this.controllerModule = controllerModule;
	}

	public String getFullClassName(String packageName, String className) {
		return packageName + "." + className;
	}
}
